<template>
  <div
    v-if="caritem"
    data-test="patroling-top"
    class="h-full flex items-center relative"
  >
    <div
      class="flex-1 flex items-center cursor-pointer select-none"
      @click="modeSelectVisible = !modeSelectVisible"
    >
      <SvgIcon class="w-10" :name="`patroling-${currentModeIcon}`"></SvgIcon>
      <span class="text-orange-400 ml-2">{{ currentModeText }}</span>
    </div>
    <div class="flex-1">
      <span class="text-[#91EDFF] mr-2">底盘：</span>
      <span class="text-orange-400">{{ baseModeText }}</span>
    </div>
    <template v-if="mode === modes.MANUAL">
      <div class="flex-1">
        <span class="text-[#91EDFF] mr-2">速度：</span>
        <span class="text-orange-400">{{ speed }}</span>
      </div>
      <div class="flex-1">
        <span class="text-[#91EDFF] mr-2">转向：</span>
        <span class="text-orange-400">{{ direction }}</span>
      </div>
    </template>
    <div class="flex-1">
      <span class="text-[#91EDFF] mr-2">控制：</span>
      <span class="text-orange-400">{{ controlerType }}</span>
    </div>
    <div class="flex-1 flex items-center">
      <span class="text-[#91EDFF]">前灯：</span>
      <el-switch v-model="frontLight" active-color="#ff931e" />
    </div>
    <div class="flex-1 flex items-center">
      <span class="text-[#91EDFF]">后灯：</span>
      <el-switch v-model="backLight" active-color="#ff931e" />
    </div>
    <div class="flex-1 flex items-center">
      <span class="text-[#91EDFF]">语音：</span>
      <el-switch v-model="voice" active-color="#ff931e" />
    </div>
    <div class="flex-1 flex items-center">
      <span class="text-[#91EDFF]">停止：</span>
      <el-switch v-model="stopMode" active-color="#ff931e" />
    </div>
    <div class="flex-1 flex items-center">
      <span class="text-[#91EDFF]">激光驱散器：</span>
      <el-switch v-model="disperseMode" active-color="#ff931e" />
    </div>
    <div class="flex-1 flex items-center justify-end">
      <span class="text-[#91EDFF] mr-2">{{ battery }}%</span>
      <BatteryIcon :battery="battery" />
    </div>
    <template v-if="modeSelectVisible">
      <div class="absolute top-20 left-0 z-[1003] bg-[#072232] text-white w-96">
        <div
          v-for="item in modeList"
          :key="item.value"
          class="flex items-center p-5 w-full justify-between cursor-pointer hover:bg-[#1f3947]"
          :class="{ isSelect: currentModeSelect(item.value) }"
          @click="changeMode(item.value)"
        >
          <div class="flex items-center">
            <SvgIcon
              class="w-10 h-10 mr-5"
              :name="`patroling-${item.icon}`"
            ></SvgIcon>
            <span class="justify-self-start">{{ item.title }}</span>
          </div>
          <SvgIcon
            class="w-8 h-8 mr-5"
            :name="`patroling-${
              currentModeSelect(item.value) ? 'radio_s' : 'radio_n'
            }`"
          ></SvgIcon>
        </div>
      </div>
    </template>
  </div>
</template>

<script setup lang="ts">
import { computed, ComputedRef, watch } from "vue";
import usePatrolingStore from "@/store/patroling";
import { storeToRefs } from "pinia";
import BatteryIcon from "./BatteryIcon.vue";
import useGamepad from "./composables/useGamepad";
import useCarMove from "./composables/useCarMove";
import useModeControl from "./composables/useModeControl";
import { modes } from "./shared";
import useLightControl from "./composables/useLightControl";
import useVoiceControl from "./composables/useVoiceControl";
import usePantilControl from "./composables/usePantilControl";
import useLaserControl from "./composables/useLaserControl";
import useAwayControl from "./composables/useAwayControl";

const { patrolingInfo } = storeToRefs(usePatrolingStore());
const caritem: ComputedRef<string> = computed(() => {
  return patrolingInfo.value.robotid;
});

const {
  initGamepad,
  controlerType,
  gear,
  speed,
  direction,
  disconnectGamepad,
  clickedButton,
  pantiltDirection,
} = useGamepad();

const {
  mode,
  currentModeIcon,
  modeSelectVisible,
  modeList,
  currentModeText,
  currentModeSelect,
  changeMode,
  toggleStopMode,
  baseModeText,
  stopMode,
} = useModeControl({
  patrolingInfo,
  gear,
  caritem,
});

useCarMove({ speed, direction, caritem, mode });

watch(caritem, () => {
  disconnectGamepad();
  initGamepad();
});

const { frontLight, backLight, toggleFrontLight, toggleBackLight } =
  useLightControl({ caritem });

const battery: ComputedRef<number> = computed(() => {
  return patrolingInfo.value.battery || 0;
});

const { voice } = useVoiceControl({ caritem });

const { disperseMode, toggleLaser } = useLaserControl({ caritem });

function toggleGear() {
  gear.value = !gear.value;
}

const { togglePersonAway, toggleBirdAway } = useAwayControl({ caritem });

const actionMap: ComputedRef<object> = computed(() => {
  if (controlerType.value === "方向盘") {
    return {
      0: togglePersonAway,
      1: toggleLaser,
      2: toggleBirdAway,
      3: stopPantilt,
      6: toggleGear,
      7: toggleFrontLight,
      10: toggleStopMode,
      11: toggleBackLight,
    };
  } else {
    return {
      0: stopPantilt,
      1: toggleBirdAway,
      2: togglePersonAway,
      3: toggleLaser,
      4: toggleStopMode,
      5: toggleGear,
      6: toggleFrontLight,
      7: toggleBackLight,
    };
  }
});

watch(clickedButton, (i: number) => {
  if (i === null) return;
  const actionGetter = actionMap.value[i];
  actionGetter && actionGetter();
  clickedButton.value = null;
});

const { stopPantilt } = usePantilControl({ caritem, pantiltDirection });
</script>

<style lang="scss" scoped>
.isSelect {
  border: 1px solid #12feff;
}
</style>
